<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="75px">
<!--      <el-form-item label="购买课程" prop="productId" v-if="!platformId">
        <el-select v-model="queryParams.productId" placeholder="请选择课程" clearable>
          <el-option
            v-for="item in platformList"
            :key="item.baseId"
            :label="item.title"
            :value="item.baseId">
            <span style="float: left">{{ item.title }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">
              <dict-tag :options="dict.type.bmzt" :value="item.applyStatus"/>
            </span>
          </el-option>
        </el-select>
      </el-form-item>-->
      <el-form-item label="手机号" prop="productPhone" v-if="!createBy">
        <el-input
          v-model="queryParams.productPhone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发票抬头" prop="invoiceTt">
        <el-input
          v-model="queryParams.invoiceTt"
          placeholder="请输入发票抬头"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收件人" prop="dispatchReceiver">
        <el-input
          v-model="queryParams.dispatchReceiver"
          placeholder="请输入收件人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮箱" prop="dispatchEmail">
        <el-input
          v-model="queryParams.dispatchEmail"
          placeholder="请输入邮箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择支付状态" clearable>
          <el-option
            v-for="item in orderStatusList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:orderInfo:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="订单主键" align="center" prop="baseId" v-if="false"/>
      <el-table-column label="订单编号" align="center" prop="orderNo" />
      <el-table-column label="购买课程" align="center" prop="productName" :show-overflow-tooltip="true"/>
      <el-table-column label="苏学码" align="center" prop="sxmCode" :show-overflow-tooltip="true"/>
      <el-table-column label="订单金额(元)" align="center" prop="productTotal"/>
      <el-table-column label="发票类型" align="center" prop="invoiceType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.fplx" :value="scope.row.invoiceType"/>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="productPhone"/>
      <el-table-column label="邮箱" align="center" prop="dispatchEmail"/>
      <el-table-column label="支付账号" align="center" prop="createBy" />
      <el-table-column label="创建订单时间" align="center" prop="createTime" />
      <el-table-column label="订单状态" align="center" prop="orderStatus"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
          >详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-warning-outline"
            @click="handleRefund(scope.row)"
          >退款
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 订单管理详情 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-descriptions title="订单信息" :column="3" size="medium" border>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-user"></i>
            支付账号
          </template>
          {{ form.createBy }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-mobile-phone"></i>
            手机号
          </template>
          {{ form.productPhone }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            创建订单时间
          </template>
          {{ form.createTime }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            单价
          </template>
          {{ form.productSellPrice }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            数量
          </template>
          {{ form.productCount }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            订单金额
          </template>
          {{ form.productTotal }}元
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            订单状态
          </template>
          {{ form.orderStatus }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            购买课程
          </template>
          {{ form.productName }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            苏学码
          </template>
          {{ form.sxmCode }}
        </el-descriptions-item>

      </el-descriptions>
      <el-descriptions class="margin-top" title="开票信息" :column="3" size="medium" border>

        <el-descriptions-item>
          <template slot="label">
            发票类型
          </template>
          <dict-tag :options="dict.type.fplx" :value="form.invoiceType"/>
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            发票金额
          </template>
          {{ form.invoiceAmount }}元
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            收件人
          </template>
          {{ form.dispatchReceiver }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            邮箱
          </template>
          {{ form.dispatchEmail }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            发票抬头
          </template>
          {{ form.invoiceTt }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            发票内容
          </template>
          {{ form.invoiceContent }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            发票税号
          </template>
          {{ form.invoiceSh }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            开户银行
          </template>
          {{ form.invoiceBank }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            开户账户
          </template>
          {{ form.invoiceAccount }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            单位地址
          </template>
          {{ form.invoiceAddress }}
        </el-descriptions-item>

      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listOrderInfo, getOrderInfo, delOrderInfo, addOrderInfo, updateOrderInfo} from "@/api/business/orderInfo";
import {platformList} from "@/api/business/platform";

export default {
  name: "OrderInfo",
  dicts: ['fplx', 'zffs', 'bmzt'],
  props: {
    platformId: {
      type: String,
      default: ""
    },
    createBy: {
      type: String,
      default: ""
    },
    activeName: {
      type: String,
      default: ""
    },
  },
  watch: {
    platformId: {
      handler: function (val, oldVal) {
        this.queryParams.productId = this.platformId;
        this.queryParams.createBy = this.createBy;
        this.getList();
      },
      immediate: true
    },
    createBy: {
      handler: function (val, oldVal) {
        this.queryParams.productId = this.platformId;
        this.queryParams.createBy = this.createBy;
        this.getList();
      },
      immediate: true
    },
    activeName: {
      handler: function (val, oldVal) {
        this.queryParams.productId = this.platformId;
        this.queryParams.createBy = this.createBy;
        if (val === 'first') {
          this.getList();
        }
      },
      immediate: true
    }
  },
  data() {
    return {
      orderStatusList: [
        {label: '未支付', value: '未支付'},
        {label: '支付成功', value: '支付成功'},
        {label: '超时已关闭', value: '超时已关闭'},
        {label: '退款中', value: '退款中'},
        {label: '已退款', value: '已退款'},
      ],
      // 课程列表
      platformList: [],

      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单管理表格数据
      orderInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productId: this.platformId,
        createBy: this.createBy,
        productSellPrice: undefined,
        productCount: undefined,
        productPhone: undefined,
        productPayMethod: undefined,
        productTotal: undefined,
        invoiceType: undefined,
        invoiceTt: undefined,
        invoiceTtType: undefined,
        invoiceContent: undefined,
        invoiceAmount: undefined,
        invoiceSh: undefined,
        invoiceBank: undefined,
        invoiceAccount: undefined,
        invoiceAddress: undefined,
        dispatchReceiver: undefined,
        dispatchEmail: undefined,
        codeUrl: undefined,
        orderStatus: undefined,
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
    if (!this.platformId) {
      this.getPlatformList();
    }
  },
  methods: {
    getPlatformList() {
      platformList().then(response => {
        this.platformList = response.data;
      });
    },
    /** 查询订单管理列表 */
    getList() {
      this.loading = true;
      listOrderInfo(this.queryParams).then(response => {
        this.orderInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        baseId: undefined,
        productId: undefined,
        productName: undefined,
        productSellPrice: undefined,
        productCount: undefined,
        productPhone: undefined,
        productPayMethod: undefined,
        productTotal: undefined,
        invoiceType: undefined,
        invoiceTt: undefined,
        invoiceTtType: undefined,
        invoiceContent: undefined,
        invoiceAmount: undefined,
        invoiceSh: undefined,
        invoiceBank: undefined,
        invoiceAccount: undefined,
        invoiceAddress: undefined,
        dispatchReceiver: undefined,
        dispatchEmail: undefined,
        codeUrl: undefined,
        sxmCode: undefined,
        orderStatus: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.baseId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单管理";
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.loading = true;
      this.reset();
      const baseId = row.baseId || this.ids
      getOrderInfo(baseId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "订单编号：" + row.orderNo;
      });
    },
    handleRefund(row) {
      this.$modal.prompt('请输入退款原因：')
        .then(({value}) => {
          // if (!(row.productPhone === value)) {
          //   this.$modal.alertError("输入内容不正确");
          //   return;
          // }
          if (!value) {
            this.$modal.alertWarning("请输入退款原因！");
            return;
          }
          this.$modal.msgSuccess("操作成功");
          this.loading = true;
          return delOrderInfo(row.orderNo, value);
        }).then(() => {
        this.loading = false;
        this.getList();
        // this.$modal.msgSuccess("操作成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const baseIds = row.baseId || this.ids;
      this.$modal.confirm('是否确认删除订单管理编号为"' + baseIds + '"的数据项？').then(() => {
        this.loading = true;
        return delOrderInfo(baseIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/orderInfo/export', {
        ...this.queryParams
      }, `orderInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.margin-top {
  margin-top: 20px;
}
</style>
